package cn.bluemobi.dylan.step.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.step.bean.Weight;

public class WeightRecordAdapter extends BaseAdapter {
    private Context context;
    private List<Weight> weightList;
    private ViewHolder viewHolder;
    private int convertViewCount;
    public WeightRecordAdapter(Context context,List<Weight> weightList){
        this.context=context;
        this.weightList=weightList;
    }
    @Override
    public int getCount() {
        return weightList.size();
    }

    //获取列表对象
    @Override
    public Object getItem(int position) {
        return weightList;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //获取列表项视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.weight_list_item,parent,false);
            convertViewCount++;
            //实例化缓存类对象
            viewHolder = new ViewHolder();
            viewHolder.dataTextView = convertView.findViewById(R.id.weight_date);
            viewHolder.weightTextView = convertView.findViewById(R.id.weight_weight);
            convertView.setTag(viewHolder);
            convertView.setId(convertViewCount);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.weightTextView.setText(weightList.get(position).getWeight());
        viewHolder.dataTextView.setText(weightList.get(position).getDate());
        return convertView;
    }

    //ViewHolder缓存类
    class ViewHolder{
        TextView dataTextView;
        TextView weightTextView;
    }
}
